package org.gg.boom.modules.sys.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.gg.boom.common.controller.BaseController;
import org.gg.boom.common.model.Response;
import org.gg.boom.common.utils.poi.ExcelUtil;
import org.gg.boom.kernel.annotation.Log;
import org.gg.boom.kernel.enums.BusinessType;
import org.gg.boom.modules.sys.model.param.SysConfigParam;
import org.gg.boom.modules.sys.model.vo.SysConfigResult;
import org.gg.boom.modules.sys.service.SysConfigService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 参数配置 信息操作处理
 */
@RestController
@RequestMapping("/system/config")
public class SysConfigController extends BaseController {

    @Resource
    private SysConfigService configService;

    /**
     * 获取参数配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:config:list')")
    @GetMapping("/list")
    public Response list(SysConfigParam param) {
        Page<SysConfigResult> page = configService.selectConfigList(param);
        return getDataTable(page);
    }

    @Log(title = "参数管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:config:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysConfigParam param) {
        Page<SysConfigResult> page = configService.selectConfigList(param);
        ExcelUtil<SysConfigResult> util = new ExcelUtil<>(SysConfigResult.class);
        util.exportExcel(response, page.getRecords(), "参数数据");
    }

    /**
     * 根据参数编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:config:query')")
    @GetMapping(value = "/{configId}")
    public Response getInfo(@PathVariable Long configId) {
        return success(configService.selectConfigById(configId));
    }

    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "/configKey/{configKey}")
    public Response getConfigKey(@PathVariable String configKey) {
        return success(configService.selectConfigByKey(configKey));
    }

    /**
     * 新增参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:add')")
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping
    public Response add(@Validated @RequestBody SysConfigParam param) {
        if (!configService.checkConfigKeyUnique(param)) {
            return error("新增参数'" + param.getConfigName() + "'失败，参数键名已存在");
        }
        return toResponse(configService.insertConfig(param));
    }

    /**
     * 修改参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:edit')")
    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public Response edit(@Validated @RequestBody SysConfigParam param) {
        if (!configService.checkConfigKeyUnique(param)) {
            return error("修改参数'" + param.getConfigName() + "'失败，参数键名已存在");
        }
        return toResponse(configService.updateConfig(param));
    }

    /**
     * 删除参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public Response remove(@PathVariable Long[] configIds) {
        configService.deleteConfigByIds(configIds);
        return success();
    }

    /**
     * 刷新参数缓存
     */
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "参数管理", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public Response refreshCache() {
        configService.resetConfigCache();
        return success();
    }
}
